package com.cuzofu.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuzofu.fs.common.PageUtils;
import com.cuzofu.fs.common.Query;
import com.cuzofu.fs.dao.UnitDao;
import com.cuzofu.fs.entity.MajorEntity;
import com.cuzofu.fs.entity.UnitEntity;
import com.cuzofu.fs.service.MajorService;
import com.cuzofu.fs.service.UnitService;
import com.cuzofu.fs.vo.UnitVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("unitService")
public class UnitServiceImpl extends ServiceImpl<UnitDao, UnitEntity> implements UnitService {

    @Autowired
    private MajorService majorService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UnitEntity> page = this.page(
                new Query<UnitEntity>().getPage(params),
                new QueryWrapper<UnitEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 创建单体工程（同时初始化5个专业记录）
     * @param vo
     * @return
     */
    @Override
    @Transactional
    public boolean createUnit(UnitVO vo) {

        boolean isNew = true;
        if (StringUtils.isNotBlank(vo.getId())) {
            isNew = false;
        }
        UnitEntity unit = new UnitEntity();
        unit.setPid(vo.getPid());
        unit.setName(vo.getName());
        this.saveOrUpdate(unit);

        if (isNew) {

            String pid = unit.getPid();
            String uid = unit.getId();
            List<MajorEntity> list = new ArrayList<>();

            MajorEntity jianzhu = new MajorEntity();
            jianzhu.setName("建筑");
            jianzhu.setSort(1);
            jianzhu.setPid(pid);
            jianzhu.setUid(uid);
            list.add(jianzhu);

            MajorEntity jiegou = new MajorEntity();
            jiegou.setName("结构");
            jiegou.setSort(2);
            jiegou.setPid(pid);
            jiegou.setUid(uid);
            list.add(jiegou);

            MajorEntity jipaishui = new MajorEntity();
            jipaishui.setName("给排水");
            jipaishui.setSort(3);
            jipaishui.setPid(pid);
            jipaishui.setUid(uid);
            list.add(jipaishui);

            MajorEntity nuantong = new MajorEntity();
            nuantong.setName("暖通");
            nuantong.setSort(4);
            nuantong.setPid(pid);
            nuantong.setUid(uid);
            list.add(nuantong);

            MajorEntity dianqi = new MajorEntity();
            dianqi.setName("电气");
            dianqi.setSort(5);
            dianqi.setPid(pid);
            dianqi.setUid(uid);
            list.add(dianqi);
            majorService.saveBatch(list);

        }
        return true;
    }


}